package com.work.formaldehyde.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.work.formaldehyde.R;
import com.work.formaldehyde.model.AllDingDanModel;
import com.work.formaldehyde.orderfragment.AllFragment;
import com.work.formaldehyde.orderfragment.DPJFragment;
import com.work.formaldehyde.orderfragment.DSHFragment;
import com.work.formaldehyde.orderfragment.DZFFragment;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.AppManager;
import com.work.formaldehyde.util.Url;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;


public class DingDanXiangQingActivity extends Activity implements View.OnClickListener {
    private ImageView comeback, gouwuche_img;
    private RelativeLayout quzhifu;
    private TextView title, phone, address_msg;
    private LinearLayout addview;
    private TextView shangpingmingzi, gouwuche_money, spzj, hdyh, yf, ddzj, ddbh, xdsj, zffs, zfsj, sfk;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApiUtils.ActivityIsLogin(this, 1);
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_dingdanxiangqing);
        comeback = findViewById(R.id.querendingdan_comeback);
        comeback.setOnClickListener(this);
        if (ApiUtils.isNetworkConnected(DingDanXiangQingActivity.this))
            init();
    }

    private String dingdanhao = "", shangpingname, shangpingnum, shangpingimg;
    private String duoge = "false", type = "", ispay = "";
    private int position;
    double money, moneyall = 0;

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private String tuihuo;

    /**
     * 初始加载
     */
    public void init() {
        Intent getstr = getIntent();
        tuihuo = getstr.getStringExtra("tuihuo");
        duoge = getstr.getStringExtra("duoge");
        ispay = getstr.getStringExtra("ispay");
        type = getstr.getStringExtra("type");
        position = Integer.parseInt(getstr.getStringExtra("position"));
        addview = findViewById(R.id.addview);
        gouwuche_img = findViewById(R.id.gouwuche_img);
        ApiUtils.GET_IMAGE(DingDanXiangQingActivity.this, getstr.getStringExtra("shangpingimg"), gouwuche_img);
        shangpingimg = getstr.getStringExtra("shangpingimg");
        quzhifu = findViewById(R.id.quzhifu);
        quzhifu.setOnClickListener(this);
        title = findViewById(R.id.title);
        title.setText(getstr.getStringExtra("username"));
        phone = findViewById(R.id.phone);
        phone.setText(getstr.getStringExtra("userphone"));
        address_msg = findViewById(R.id.address_msg);
        address_msg.setText(getstr.getStringExtra("address"));
        shangpingmingzi = findViewById(R.id.shangpingmingzi);
        shangpingmingzi.setText(getstr.getStringExtra("shangpingname"));
        shangpingname = getstr.getStringExtra("shangpingname");
        gouwuche_money = findViewById(R.id.gouwuche_money);
        gouwuche_money.setText("x" + getstr.getStringExtra("shangpingnum"));
        shangpingnum = getstr.getStringExtra("shangpingnum");
        spzj = findViewById(R.id.spzj);
        spzj.setText(getstr.getStringExtra("shangpingzongjia") + "元");
        hdyh = findViewById(R.id.hdyh);
        yf = findViewById(R.id.yf);
        sfk = findViewById(R.id.sfk);
        ddzj = findViewById(R.id.ddzj);
        ddbh = findViewById(R.id.ddbh);
        xdsj = findViewById(R.id.xdsj);
        zffs = findViewById(R.id.zffs);
        zfsj = findViewById(R.id.zfsj);
        hdyh.setText("0元");
        yf.setText("0元");
        money = Double.parseDouble(String.valueOf(Double.parseDouble(getstr.getStringExtra("shangpingzongjia")) * Double.parseDouble(getstr.getStringExtra("shangpingnum"))));
        ddzj.setText(money + "元");
//        sfk.setText(money + "元");
        ddbh.setText(getstr.getStringExtra("dingdannum"));
        dingdanhao = getstr.getStringExtra("dingdannum");
        xdsj.setText(getstr.getStringExtra("xiadanshijian"));
        if (ispay.equals("0")) {
            sfk.setText("待支付");
            zffs.setText("待支付");
            zfsj.setText("待支付");
            quzhifu.setVisibility(View.GONE);
        } else {
            sfk.setText(money + "元");
            zffs.setText(getstr.getStringExtra("zhifufangshi"));
            zfsj.setText(getstr.getStringExtra("zhifushijian"));
        }
        if (duoge.equals("true")) {
            if (type.equals("0")) {
                create(AllFragment.new_list_views, position);
            } else if (type.equals("1")) {
                create(DZFFragment.new_list_views, position);
            } else if (type.equals("2")) {
                create(DSHFragment.new_list_views, position);
            } else {
                create(DPJFragment.new_list_views, position);
            }
        }
        ApiUtils.SetLog("zhuangtai:" + tuihuo);
        if (!tuihuo.equals("1") && !tuihuo.equals("2")) {
            quzhifu.setVisibility(View.GONE);
            ApiUtils.SetLog("zhuangtai1:" + tuihuo);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.querendingdan_comeback:
                onBackPressed();
                break;
            case R.id.quzhifu:
                if (tuihuo.equals("4")) {
                    ApiUtils.SetToast(DingDanXiangQingActivity.this, "该订单已经申请退货了");
                } else {
                    Intent qxdd = new Intent(DingDanXiangQingActivity.this, QuXiaoDingDanActivity.class);
                    qxdd.putExtra("dingdannum", dingdanhao);
                    qxdd.putExtra("dingdanallmoney", money + "");
                    qxdd.putExtra("shangpingname", shangpingname);
                    qxdd.putExtra("shangpingnum", shangpingnum);
                    qxdd.putExtra("shangpingimg", shangpingimg);
                    startActivity(qxdd, ActivityOptions.makeSceneTransitionAnimation(DingDanXiangQingActivity.this).toBundle());
                    onBackPressed();
                }
//                okHttp_postFromParameters1();
                break;
        }
    }

    public void create(ArrayList<AllDingDanModel.data> list, int position) {
        for (int creview = 1; creview < list.get(position).getGoods().size(); creview++) {
            LinearLayout relativeLayout = new LinearLayout(DingDanXiangQingActivity.this);
            LinearLayout relativeLayouts = new LinearLayout(DingDanXiangQingActivity.this);
            ImageView imageView = new ImageView(DingDanXiangQingActivity.this);
            TextView textView = new TextView(DingDanXiangQingActivity.this);
            TextView textViews = new TextView(DingDanXiangQingActivity.this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams lps = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams lpimg = new LinearLayout.LayoutParams(gouwuche_img.getLayoutParams().width, gouwuche_img.getLayoutParams().height);
            LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams lp1s = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lps.setMargins(24, 0, 0, 0);
            lpimg.setMargins(14, 0, 0, 0);
            lp1s.setMargins(4, 4, 4, 4);
            lp1.setMargins(0, 12, 0, 0);
            relativeLayout.setLayoutParams(lp);
            relativeLayout.setOrientation(LinearLayout.HORIZONTAL);
            relativeLayouts.setLayoutParams(lps);
            relativeLayouts.setOrientation(LinearLayout.VERTICAL);
            imageView.setLayoutParams(lpimg);
            ApiUtils.GET_IMAGE(DingDanXiangQingActivity.this, list.get(position).getGoods().get(creview).getGoods_img(), imageView);
            textView.setLayoutParams(lp1);
            textView.setLines(2);
            textView.setEllipsize(TextUtils.TruncateAt.END);
            textView.setText(list.get(position).getGoods().get(creview).getGoods_name());
            textView.setTextColor(getResources().getColor(R.color.text_black));
            textView.setTextSize(14);
            textViews.setLayoutParams(lp1);
            textViews.setText("x" + list.get(position).getGoods().get(creview).getGoods_num());
            textViews.setTextColor(getResources().getColor(R.color.home_bottom_text_color_ischeck));
            textViews.setTextSize(11);
            moneyall = Double.parseDouble(String.valueOf(Double.parseDouble(list.get(position).getGoods().get(creview).getGoods_discount_price()) * Double.parseDouble(list.get(position).getGoods().get(creview).getGoods_num())));
            money += moneyall;
            ddzj.setText(money + "元");
            sfk.setText(money + "元");
            relativeLayout.addView(imageView);
            relativeLayout.addView(relativeLayouts);
            relativeLayouts.addView(textView);
            relativeLayouts.addView(textViews);
            addview.addView(relativeLayout);
        }
    }

}
